package com.ibsol.database
{
	import com.ibsol.model.Join;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLEvent;
	import mx.collections.ArrayCollection;
	
	
	public class JoinDAO
	{
		private var _tourixDBConn:SQLConnection;
		
		public function JoinDAO()
		{
			_tourixDBConn = DBConnManager.getConnection("tourix");
		}
		
		public function loadJoinData(resultHandler:Function):void
		{
			var sqlStr:SQLStatement = new SQLStatement;
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "SELECT p.name, i.name as iname FROM package as p, itinerary as i";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					resultHandler.call(this, typeArray(sqlStr.getResult().data));
				});
			sqlStr.execute();
		}
		
		private function typeObject(o:Object):Join
		{
			var j:Join = new Join();
			j.pname = o.j.name;
			j.iname = o.i.name;
			return j;
		}
		
		/**
		 * Converts array object to Marker array collection
		 * @param: a:Array array of objects returned by SQL
		 * @return: ArrayCollection of Marker after conversion from Array
		 */
		private function typeArray(a:Array):ArrayCollection
		{
			if (a==null) return null;
			var ac:ArrayCollection = new ArrayCollection();
			for (var i:int; i<a.length; i++)
			{
				ac.addItem(typeObject(a[i]));
			}
			return ac;
		}
	}
}